package plsqlfs;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public final class ByteStreams {

	private ByteStreams() {
		
	}

	public static final int DEFAULT_BUFFER_SIZE = 1024;

	public static void copy(InputStream source, OutputStream destination) throws IOException {
		copy( source, destination, DEFAULT_BUFFER_SIZE );
	}

	public static void copy(InputStream source, OutputStream destination, int bufferSize) throws IOException {
		byte[] buffer = new byte[ bufferSize ];
		
		int bytesRead;
		while ( (bytesRead = source.read(buffer)) != -1 ) {
			destination.write( buffer, 0, bytesRead );
		}
	}

	public static void copy(File source, File destination) throws IOException {
		InputStream inputStream = new FileInputStream(source);
		try {
			OutputStream outputStream = new FileOutputStream(destination);
			try {
				copy(inputStream, outputStream);
			} finally {
				outputStream.close();
			}
		} finally {
			inputStream.close();
		}
	}

}
